package hermesV3.services.TupleSpace.examples;

import hermesV3.services.TupleSpace.Space;
import hermesV3.services.TupleSpace.Tuple;
import hermesV3.services.TupleSpace.TupleException;

import org.sercho.masp.space.TupleSpace;

/**
 * 
 * @author Ambruoso Luigi
 * @author Addeo Vincenzo
 * 
 */

public class TestS {

	/**
	 * @param args sadsads
	 */
	public static void main(String[] args) {
		TupleSpace<Tuple> space = Space.getInstance();
		Contatto c1 = new Contatto();
		c1.setNome("Pasquale");
		c1.setCognome("Scognamilio");
		Contatto c2 = new Contatto();
		c2.setNome("Giuseppe");
		c2.setCognome("Scognamilio");
		Contatto c3 = new Contatto();
		c3.setNome("Pasquale");
		c3.setCognome("Caso");
		ContattoMail cM1 = new ContattoMail();
		cM1.setNome("Luigi");
		cM1.setCognome("Barbuto");
		cM1.setMail("luigi@mail.com");

		Tuple tt = new Tuple();
		Tuple t1 = new Tuple();
		tt.addKey(Contatto.CONTATTO);
		try {
			tt.addField(Contatto.NOME, "Pasquale");
		} catch (TupleException e) {

			e.printStackTrace();
		}
		Tuple tcontatti = new Tuple();
		tcontatti.addKey(Contatto.CONTATTO);

		space.write(c2);
		space.write(c3);
		space.write(cM1);
		space.write(c1);

		// coordinate
		Cordinate cc1 = new Cordinate();
		cc1.setX(new Integer(3));
		cc1.setY(new Integer(4));
		cc1.setZ(new Integer(7));

		Cordinate cc2 = new Cordinate();
		cc2.setX(new Integer(89));
		cc2.setY(new Integer(4));
		cc2.setZ(new Integer(15));

		Cordinate cc3 = new Cordinate();
		cc3.setX(new Integer(90));
		cc3.setY(new Integer(45));
		// cc3.setZ(87);

		space.write(cc1);
		space.write(cc2);
		space.write(cc3);

		Tuple tc = new Tuple();
		tc.addKey(Cordinate.CORDINATE);
		try {
			tc.addField(Cordinate.X, new Integer(3));
		} catch (TupleException e) {

			e.printStackTrace();
		}

		System.out.println("------Tutti-------");
		System.out.println(space.readAll(t1));
		System.out.println("------Tutti i Contatti Pasquale-------");
		System.out.println(space.readAll(tt));
		System.out.println("------Tutte le Cordinate x=3-------");
		System.out.println(space.readAll(tc));
		System.out.println("------leggo e rimuovo Tutti i Contatti -------");
		System.out.println(space.removeAll(tcontatti));
		System.out.println("------Tutti-------");
		System.out.println(space.readAll(t1));

		System.out.println("------rimuovo Tutti-------");

		while (!space.readAll(t1).isEmpty()) {
			System.out.println(space.readAll(t1));
			int i = space.readAll(t1).size();
			space.remove(t1, 10000);
			System.out.println("------@@@@@@-------");
			while (i == space.readAll(t1).size()) {
				// DO NOTHING
			}
		}

	}

}
